const express = require('express')
const loopRouter = express.Router()
const fs = require('fs')

loopRouter.get('/loopData', (req, res) => {
    let loopData = require('../data/loopData.json') 
    res.send({ status: 0, message: '获取图书成功', data: loopData })
})
loopRouter.post('/loopadd',(req,res)=>{
    let loopData =require('../data/loopData.json') 
    loopData.push({...req.body,id:Date.now()})
   fs.writeFile('./data/loopData.json', JSON.stringify(loopData),(error,data)=>{
    res.send({ status: 0, message: '添加成功' })
    })
   
})
loopRouter.post('/loopdel', (req,res)=>{
    let loopData =require('../data/loopData.json') 
        let index =  loopData.findIndex((item)=>req.body.id==item.id)
        loopData.splice(index,1)
        console.log(loopData);
     fs.writeFile('./data/loopData.json', JSON.stringify(loopData),(error,data)=>{
        res.send({ status: 0, message: '删除成功'})
    })
    
})
loopRouter.get('/showdata',(req,res)=>{
    let showdata = require('../data/showData.json')
    res.send({ status: 0, message: '获取漫展成功', data: showdata })
})
loopRouter.post('/showdel', (req,res)=>{
    let showData =require('../data/showData.json') 
        let index =  showData.findIndex((item)=>req.body.id==item.id)
        showData.splice(index,1)
     fs.writeFile('./data/showData.json', JSON.stringify(showData),(error,data)=>{
        res.send({ status: 0, message: '删除成功'})
    })
    
})
loopRouter.post('/showadd',(req,res)=>{
    let showData =require('../data/showData.json') 
    showData.push({...req.body,id:Date.now()})
   fs.writeFile('./data/showData.json', JSON.stringify(showData),(error,data)=>{
    res.send({ status: 0, message: '添加成功' })
    })
   
})
module.exports = loopRouter